import fs from 'fs';
import {resolve} from 'path';
const rootDir = resolve(process.cwd(), 'src')


/**
 * @name 通过传入的name名字去src获取对应的文件夹，并转换为menu下拉菜单
 * @param {String} dirname
 * @return [{text: dirname, link: '/src/dirname' }]
 */
function getMenu(dirname) {
  let fileList = fs.readdirSync(resolve(rootDir, dirname))
  return fileList.map(item => ({text: item, link: `/src/${dirname}/${item}/`}))
}


function getSidebar(menuArr) {
  const sidebar = {};
	menuArr.forEach(item => {
		sidebar[item.link] = [{
			text: item.text,
			items: getFile(item.text)
		}]
	})
  return sidebar;
}
function getFile (dirname) {
	let fileList = fs.readdirSync(resolve(rootDir, 'fe', dirname))
	// 去除index和文件夹
	fileList = fileList.filter(item => (!item.includes('index') && item.includes('.')))
	return fileList.map(item => {
    const fileName = item.replace(/\.md/, '');
    return {
      text: fileName,
      link: `/src/fe/${dirname}/${fileName}`
    }
  })
}


// 通过文件夹获取菜单
const feLinks = getMenu('fe');
const sidebar = getSidebar(feLinks)
export default { feLinks, sidebar };